package cn.com.note.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import cn.com.note.R;
import cn.com.note.view.MenuPop;

/**
 * Created by sunfulin on 2019/1/8.
 */

public class TitleBarActivity extends AppCompatActivity implements View.OnClickListener {
    MenuPop menuPop;
    Button bt1,bt2,bt3;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_title_bar);
        init();
        initView();
        initData();
    }

    private void init() {
    }


    private void initView() {
        bt1=findViewById(R.id.bt1);
        bt2=findViewById(R.id.bt2);
        bt3=findViewById(R.id.bt3);

    }

    private void initData() {
        List<String> list = new ArrayList<>();
        list.add("数据传输");
        list.add("设置时间");
        list.add("关于系统");
        menuPop = new MenuPop(this, list);
        menuPop.setCallBack(new MenuPop.CallBack() {
            @Override
            public void click(int position, String content) {
                Toast.makeText(TitleBarActivity.this, "点击了" + position + "个" + content, Toast.LENGTH_SHORT).show();
                menuPop.dismissPop();
            }
        });
    }
    int tag=0;
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_back:
                Toast.makeText(this, "fanhui", Toast.LENGTH_SHORT).show();
                break;
            case R.id.bt_more:
                //展示pop
                showPop(v);
                break;
            case R.id.bt1:
                tag=0;
                updateEnable(tag);
                break;
            case R.id.bt2:
                 tag=1;
                updateEnable(tag);
                break;
            case R.id.bt3:
                tag=2;
                updateEnable(tag);
                break;
        }
    }
    private void updateEnable(int tag){
        if(menuPop.getIsEnable(tag)){
            menuPop.updateUIEnable(tag,false);
        }else{
            menuPop.updateUIEnable(tag,true);
        }
        switch (tag){
            case 0:
                updateBt(bt1,menuPop.getIsEnable(tag));
                break;
            case 1:
                updateBt(bt2,menuPop.getIsEnable(tag));
                break;
            case 2:
                updateBt(bt3,menuPop.getIsEnable(tag));
                break;
        }
    }
    private void updateBt(Button button,boolean isEnable){
        button.setText(isEnable?"禁用":"启用");
        Toast.makeText(this, (isEnable?"禁用":"启用")+" 成功！", Toast.LENGTH_SHORT).show();
    }
    private void showPop(View view) {
        if (menuPop != null) {
            menuPop.showAsDropDown(view,0,0);
        }
    }
}
